package cn.kgc.conmanager.service.impl;

import cn.kgc.conmanager.mapper.ContactMapper;
import cn.kgc.conmanager.pojo.Contact;
import cn.kgc.conmanager.pojo.ContactExample;
import cn.kgc.conmanager.service.ContactService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 郭祥伟
 */
@Service
public class ContactServiceImpl implements ContactService {
    @Resource
    ContactMapper contactMapper;
    @Override
    public PageInfo<Contact> select(Integer pageNum, Integer pageSize, String cname) {
        PageHelper.startPage(pageNum,pageSize);
        List<Contact> contactList;
        if(cname!=null){
            ContactExample example=new ContactExample();
            ContactExample.Criteria criteria=example.createCriteria();
            criteria.andCnameLike("%"+cname+"%");
            contactList=contactMapper.selectByExample(example);
        }else{
            contactList=contactMapper.selectByExample(null);
        }
        PageInfo<Contact> pageInfo=new PageInfo<>(contactList);
        return pageInfo;
    }

    @Override
    public Integer add(Contact contact) {
        int i=contactMapper.insert(contact);
        return i;
    }

    @Override
    public Contact selectById(Integer id) {
        Contact contact=contactMapper.selectByPrimaryKey(id);
        return contact;
    }

    @Override
    public Integer update(Contact contact) {
        int i=contactMapper.updateByPrimaryKey(contact);
        return i;
    }

    @Override
    public Integer delete(Integer id) {
        int i=contactMapper.deleteByPrimaryKey(id);
        return i;
    }
}
